﻿using System;
using System.ServiceModel;
using ExplorerService.Common.Logging;
using ExplorerService.Communication;
using System.Diagnostics;

namespace ExplorerService
{
    /// <summary>
    /// The isolated storage explorer service singleton
    /// </summary>
    public sealed class IsolatedStorageExplorerService
    {
        /// <summary>
        /// Reference to the current instance
        /// </summary>
        static IsolatedStorageExplorerService _instance;

        /// <summary>
        /// Object used as lock for simple thread safety
        /// </summary>
        static readonly object Padlock = new object();

        /// <summary>
        /// Default constructor
        /// </summary>
        IsolatedStorageExplorerService()
        {
            //Trace.Listeners.Add(new ReflectedTraceListener());
        }

        /// <summary>
        /// Gets the instance of the IsolatedStorageExplorerService
        /// </summary>
        private static IsolatedStorageExplorerService Instance
        {
            get
            {
                lock (Padlock)
                {
                    return _instance ?? (_instance = new IsolatedStorageExplorerService());
                }
            }
        }

        /// <summary>
        /// Starts the service
        /// </summary>
        public static void Start()
        {

            Instance.StartServices();
        }

        /// <summary>
        /// Stops the service
        /// </summary>
        public static void Stop()
        {
            Instance.StopServices();
        }

        private readonly ServiceHost _applicationSessionServiceHost = new ServiceHost(typeof(ApplicationSessionService));
        private readonly ServiceHost _clientSessionServiceHost = new ServiceHost(typeof(ClientSessionService));
        private readonly ServiceHost _applicationMonitorServiceHost = new ServiceHost(typeof(ApplicationMonitorService));
        private readonly ServiceHost _applicationOperationsServiceHost = new ServiceHost(typeof(ApplicationOperationsService));
        private readonly ServiceHost _clientOperationsServiceHost = new ServiceHost(typeof(ClientOperationService));
        private readonly ServiceHost _clientStreamingServiceHost = new ServiceHost(typeof(ClientStreamingService));
        private readonly ServiceHost _applicationStreamingServiceHost = new ServiceHost(typeof(ApplicationStreamingService));


        /// <summary>
        /// Starts the explorer communication services
        /// </summary>
        private void StartServices()
        {
            _applicationSessionServiceHost.Open();
            _clientSessionServiceHost.Open();
            _applicationMonitorServiceHost.Open();
            _applicationOperationsServiceHost.Open();
            _clientOperationsServiceHost.Open();
            _clientStreamingServiceHost.Open();
            _applicationStreamingServiceHost.Open();
        }

        /// <summary>
        /// Stops the explorer communication services
        /// </summary>
        private void StopServices()
        {
            try
            {
                _applicationSessionServiceHost.Close();
                _clientSessionServiceHost.Close();
                _applicationMonitorServiceHost.Close();
                _applicationOperationsServiceHost.Close();
                _clientOperationsServiceHost.Close();
                _clientStreamingServiceHost.Close();
                _applicationStreamingServiceHost.Close();
            }
            catch (Exception)
            {

            }
        }










    }
}

